import React from 'react';
import { PopConfirm } from '@teamix/ui';
import intl from '../../../../../../../../../locale';

export default class Confirm extends React.Component {
  state = {
    visible: false,
  };

  onCancel = () => {
    this.setState({
      visible: false,
    });
  };

  onConfirm = () => {
    this.setState({
      visible: false,
    });

    this.props.onChange(false);
  };

  onVisibleChange = (visible) => {
    this.setState({
      visible,
    });
  };

  render() {
    const { visible } = this.state;
    const { needPopConfirm, trigger } = this.props;
    if (!needPopConfirm) {
      return React.cloneElement(trigger, { onChange: this.props.onChange });
    }
    return (
      <PopConfirm
        popupStyle={{
          maxWidth: 'unset',
          width: 320,
        }}
        triggerType="click"
        align="br"
        title={intl.get({
          id: 'code-assets.security.containers.switchPopConfirm.DisableIpAddressWhitelist',
          defaultMessage: '关闭 IP 白名单',
        })}
        content={intl.get({
          id: 'code-assets.security.containers.switchPopConfirm.AfterYouDisableThisFeature',
          defaultMessage: '关闭后不再验证访问 IP ，并无法使用部署密钥访问代码库，确认要关闭吗？',
        })}
        onConfirm={this.onConfirm}
        onCancel={this.onCancel}
        visible={visible}
        onVisibleChange={this.onVisibleChange}
        trigger={trigger}
      />
    );
  }
}
